// 查看详情
function tscpDet(e) {
  var jgType = $(".link___9TVqH.select___4NqDw").attr("data-val");
  window.location.href =
    "product_details.html?jgType=" + jgType + "&productid=" + e;
}

layui.use(["carousel", "form", "layer"], function () {
  var layer = layui.layer;
  var form = layui.form;
  var carousel = layui.carousel;
  // 初始化数据
  initData();

  function initData() {
    showTotal();
    getProduct({
      url: "mh/frontPage/getFrontPagePros4_bank",
    });

    // 默认银行  1:银行  2:担保 6:保险
    getOrgan({
      code: 1,
    });
  }
  // 产品tab切换
  $(".menu___1GDbd .link___9TVqH").click(function () {
    $(this).addClass("select___4NqDw").siblings().removeClass("select___4NqDw");
    // 筛选产品、修改金融机构title
    var t = $(this).text().trim();
    var v = $(this).attr("data-val");
    var url = "";
    if (t == "贷款类") {
      url = "mh/frontPage/getFrontPagePros4_bank";
      $("#jgContainer").text("银行机构");
    } else if (t == "保险类") {
      url = "mh/frontPage/getFrontPagePros4_insure";
      $("#jgContainer").text("保险机构");
    } else if (t == "担保类") {
      url = "mh/frontPage/getFrontPagePros4_db";
      $("#jgContainer").text("担保机构");
    } else {
      return;
    }
    getProduct({
      t: t,
      url: url,
    });
    getOrgan({
      code: v,
    });
  });

  // 查看更多
  $(".menu___1GDbd .query_more").click(function () {
    var code = $(".link___9TVqH.select___4NqDw").attr("data-val");
    window.location.href = "finance_product.html?code=" + code;
  });

  // 产品导航
  $(".tb_type .tb_body").click(function () {
    var fn = $(this).attr("fn");
    if (fn == "1" || fn == "2" || fn == "6") {
      window.location.href = "finance_product.html?code=" + fn;
    } else if (fn == "9") {
      var flag = sdk.myBrowser();
      //console.log("flag", flag);
      if (true) {
        // flag => true
        var user = sessionStorage.getItem("baohan_info");
        if (!user) {
          window.location.href = "finance_letter.html";
        } else {
          window.location.href = "finance_letter.html";
        }
      } else {
        layer.msg("该操作仅限于IE浏览器 !", {
          icon: 2,
          time: 1500,
        });
      }
    } else {
      layer.msg("正在建设中...", {
        icon: 4,
        time: 1500,
      });
    }
  });

  // 获取页面统计数据
  function showTotal() {
    $.ajax({
      type: "post",
      url: sdk.baseIP + "mh/frontPage/getFrontPageStatistics",
      success: function (d) {
        var status = d.status;
        var tData = d.data;
        // 展示数据
        if (status) {
          $(".main #total1").text(tData.totalEnterpriseCount || 0);
          $(".main #total2").text(tData.totalEnterpriseMoney || 0);
          $(".main #total3").text(tData.totalEnterprises || 11023);
          $(".main #total4").text(tData.totalPublishProducts || 0);
        } else {
          layer.msg("统计数据获取失败!", {
            icon: 2,
            closeBtn: 1,
            time: 3000,
          });
        }
      },
    });
    $.ajax({
      type: "get",
      url: sdk.baseIP + "zxw/jrjg/chart/indexTotal",
    });
  }

  // 获取机构产品(贷款、保险、担保、证券、基金)
  // 默认展示贷款类
  function getProduct(params) {
    var url = params.url;
    $.ajax({
      type: "post",
      url: sdk.baseIP + url,
      success: function (d) {
        //console.log('getProduct',d)
        var status = d.status;
        //var bankData = d.data; fqm-2025.3.31 删除瀚华
        var bankData = d.data.filter(item => !item.jrjgname.includes('瀚华'));
        // 展示数据
        if (status) {
          $("#listData").html(
            $.templates("#bankDiv").render({
              total: bankData.length,
              bankList: bankData,
            })
          );
        } else {
          layer.msg("数据获取失败!", {
            icon: 2,
            closeBtn: 1,
            time: 3000,
          });
        }
      },
    });
  }

  // 金融机构及分页展示
  function getOrgan(params) {
    $.ajax({
      type: "post",
      url: sdk.baseIP + "mh/jrjgPage/getGrid",
      data: {
        code: params.code,
        page: 1,
        rows: 50,
      },
      success: function (d) {
        var total = d.total;
        var rows = d.rows;
        // 展示数据
        if (total > 0) {
          var f = [];
          f = f.concat(rows);
          var p = {};
          for (var i = 1; i <= parseInt(f.length / 15 + 1); i++) {
            p["s" + i] = pagination(15, i, f);
            $("#ckvpbe" + i).html(
              $.templates("#ckvpbeTmpl").render(p["s" + i])
            );
          }
          if (i - 1 == 1) {
            $("#ckvpbe1").siblings().remove();
          } else if (i - 2 == 1) {
            $("#ckvpbe1").siblings().remove();
          }
          var ins = carousel.render({
            elem: "#carousel_bank",
            width: "100%",
            arrow: "none",
            anim: "fade",
            autoplay: true,
          });
          ins.reload({
            elem: "#carousel_bank",
            width: "100%",
            arrow: "none",
            anim: "fade",
            autoplay: true,
          });
        } else {
          layer.msg("数据获取失败!", {
            icon: 2,
            closeBtn: 1,
            time: 3000,
          });
        }
      },
    });
  }
  // 分页效果
  function pagination(pageSize, currentPage, arr) {
    var skipNum = (currentPage - 1) * pageSize; //跳过的数量
    var newArr =
      skipNum + pageSize >= arr.length
        ? arr.slice(skipNum, arr.length)
        : arr.slice(skipNum, skipNum + pageSize);
    return newArr;
  }

  var dianzibaohanModal = layer.open({
    type: 1, // page 层类型
    area: ["1124px", "800px"],
    offset: "100px",
    title: "",
    shade: 0.6, // 遮罩透明度
    //shadeClose: true, // 点击遮罩区域，关闭弹层
    //maxmin: true, // 允许全屏最小化
    closeBtn: 0,
    //id:'dianzibaohanimg',
    skin: "dianzibaohanimg",
    //scrollbar:false,
    //fixed:true,
    //anim: 0, // 0-6 的动画形式，-1 不开启
    content: '<div><img src="index/img/dianzibaohan.png" alt=""></div>',
    success: function () {
      const button = document.querySelector(".dianzibaohanimg");
      button.addEventListener("click", function () {
        //console.log("被点击了12");
        layer.close(dianzibaohanModal);
      });
    },
  });
});
